package Mapper;

import Utils.JdbcUtils;
import pojo.MouthBudget;

import java.sql.*;

public class MouthlyBudgetMapper {
    private JdbcUtils jdbcUtils = new JdbcUtils();

    public int updateMonthklyBudget(double monthklyBudget, String currentMouth) throws SQLException {
        try {
            Timestamp update = new Timestamp(System.currentTimeMillis());
            Statement statement = jdbcUtils.getStatement();
            String sql = "update monthly_budget  set budget_amount  = " + monthklyBudget + " , `update` = '" + update + "' where  year_mouth  = '" + currentMouth + "'";
            int i = statement.executeUpdate(sql);
            return i;
        } catch (SQLException e) {
            Connection conn = jdbcUtils.getConn();
            conn.rollback();
            throw new RuntimeException(e);
        }
    }

    public double getMouthlyBudget(String currentMouth) {
        double money = 0;
        try {
            Statement statement = jdbcUtils.getStatement();
            String sql = "select budget_amount from monthly_budget   where year_mouth =  '" + currentMouth + "'";
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                money = resultSet.getDouble(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return money;

    }

    public int insertMouthlyBudget(double money, String currentMouth, MouthBudget mouthBudget) {
        int i = 0;
        try {
            Statement statement = jdbcUtils.getStatement();
            String sql = "insert into monthly_budget  ( year_mouth , budget_amount , `create` , `update`) values" + "(  " + mouthBudget.getYear_mouth() + ", " + mouthBudget.getBudget_amount() + ", " + mouthBudget.getCreate() + ", " + mouthBudget.getUpdate() + ", )";
            i = statement.executeUpdate(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return i;
    }
}
